package com.apobates.jforum2.troll.notices.mapper;

import com.apobates.jforum2.troll.notices.entity.ForumLetter;
import com.apobates.jforum2.troll.notices.stub.vo.LetterInboxVo;
import com.apobates.jforum2.troll.utils.core.DateTimeUtils;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;
import java.util.Optional;
import java.util.function.Function;

public class LetterInboxVoMapperEntity implements OptionalEntityTransformMapper<ForumLetter, LetterInboxVo> {

    public static Function<ForumLetter, LetterInboxVo> mapper(){
        return forumLetter->{
            LetterInboxVo iv = new LetterInboxVo();
            iv.setSubject(forumLetter.getTitle());
            iv.setSender(forumLetter.getAuthor());
            iv.setSenderNickname(forumLetter.getNickname());
            if (0 == forumLetter.getAuthor()) {
                iv.setSenderURI("");
                iv.setRobot(true);
            } else {
                iv.setSenderURI(String.format("/member/%d.xhtml", forumLetter.getAuthor()));
                iv.setRobot(false);
            }
            iv.setDatetime(DateTimeUtils.formatClock(forumLetter.getEntryDateTime()));
            return iv;
        };
    }

    @Override
    public Optional<LetterInboxVo> transform(Optional<ForumLetter> entity) {
        return entity.map(ins->{
            return mapper().apply(ins);
        });
    }
}
